#pragma once
#include "utilities/utilities.h"
#include <QThread>
#include <QQueue>
#include <QSemaphore>
#include "utilities/PacketArray.h"
#include "CtrlThreads/IntModel.h"
#include "utilities/Locks.h"

class AppModule;
class PacketBufferBlock;
class AppModuleModel;
class GlobalStatistic;

class AppHandlerThread : public QThread , private InterruptionModel
{
	Q_OBJECT
public:
	enum HandlingInt {
		INT_INITIALIZE,
		INT_WAITFORPACKETS,
		INT_CHANGEPACKETS,
		INT_STOP,
		INT_EXIT		
	};

	AppHandlerThread(void);
	~AppHandlerThread();
	void resumeHandling();
	void stopHandling();
	void exitHandling();
	bool recvPackets(QVector<ReadablePacket*> & packets);

protected:
	void run();

private slots:
	void onGlobalStateChanged();

private:
	static const int QueueSize = 15;

	void dispatch(const QVector<ReadablePacket*>& packets);

	volatile bool m_ExitFlag;
	volatile bool m_Stopped;
	int m_Counter;
	QVector<ReadablePacket*> m_CurPackets;
	PacketArray mTmpContainer;
	PacketPtr mCurFrame;
	shared_ptr<GlobalStatistic> m_Statistic;
	
	QQueue< QVector<ReadablePacket*> > m_PacketQueue;
	QVector< shared_ptr<AppModuleModel> > m_ModulesUsingArray;
	QVector< shared_ptr<AppModuleModel> > m_ModulesUsingSingle;

	friend class WaitForPackets;
	friend class ChangeHandlingPacketsHandler;
	friend class ExitAnalyseThread;
	friend class StopHandingThread;
	friend class InitAppHandingThread;
};

